/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.knowledge.doc.util;

import com.wallellen.knowledge.doc.domain.DocType;
import com.wallellen.knowledge.doc.domain.ex.DocEntire;
import com.wallellen.lucene.adapter.DocMap;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;


/**
 * 生成全文检索时辅助生成索引元数据对象
 *
 * @author <a href="mailto:wallellen@hotmail.com">WALLE</a>
 */
public class LuceneDocUtil {
    public static DocMap getDocMap(DocEntire doc) {
        String text = "";
        if (doc.getTexts() != null) {
            text = doc.getTexts().getText1();
        }
        text = HtmlUtils.HtmlRemoveTag(text);
        DocMap map = new DocMap(doc.getDoc().getId());
        String typeAll = "";
        // 拼接所有上级分类用于索引
        if (doc.getCurrenttypes() != null) {
            for (DocType node : doc.getCurrenttypes()) {
                if (typeAll.equals("")) {
                    typeAll = node.getName();
                } else {
                    typeAll = typeAll + "/" + node.getName();
                }
            }
            map.put("TYPENAME", typeAll, Store.YES, Index.ANALYZED);
            map.put("TYPEID", doc.getType().getId(), Store.YES, Index.NO);
        }
        map.put("TAGKEY", doc.getDoc().getTagkey(), Store.YES, Index.ANALYZED);
        map.put("TITLE", doc.getDoc().getTitle(), Store.YES, Index.ANALYZED);
        map.put("AUTHOR", doc.getDoc().getAuthor(), Store.YES, Index.ANALYZED);
        map.put("DOCDESCRIBE", doc.getDoc().getDocdescribe(), Store.YES, Index.ANALYZED);
        map.put("VISITNUM", "0", Store.YES, Index.ANALYZED);
        map.put("PUBTIME", doc.getDoc().getPubtime(), Store.YES, Index.ANALYZED);
        map.put("USERID", doc.getDoc().getCuser(), Store.YES, Index.ANALYZED);
        map.put("DOMTYPE", doc.getDoc().getDomtype(), Store.YES, Index.ANALYZED);
        map.put("TEXT", text, Store.YES, Index.ANALYZED);
        return map;
    }
}
